<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Accounts;//收款账号

class Collections extends Auth
{
    //收款账号列表
    public function accounts(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            $accounts = new Accounts();
            $getaccounts = $accounts->getaccounts($res);
            echo json_encode(array('getaccounts' => $getaccounts['data'], 'size' => $getaccounts['count']));
        } elseif ($request->isGet()) {
            return view('accounts/accounts');
        }
    }

    //收款账号的添加
    public function accountsadd(Request $request)
    {
        $data = $request->param();
        $accounts = new Accounts();
        $lang = $this->lang;
        $admin = session('admin');
        if ($request->isGet()) {
            $countrys = getables('country');
            return view('accounts/accountsinfo', [
                'str' => 'accountsadd',
                'lang' => $lang,
                'countrys' => $countrys,
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getbanks'])) {
            //获取对应国家下的银行
            echo json_encode(getables('banks', ['country_id' => $data['country_id']]));
        } elseif ($request->isAjax() && $request->isPost() && isset($data['admindata'])) {
            $mapposs['position_id'] = $data['position_id'];//搜索职位id
            $mapposs['nickname'] = ['like', '%' . $data['nickname'] . '%'];
            //获取的是登录用户的子职位的用户
            $admindata = getables('admin', $mapposs, 'id,nickname');
            return array('msg' => $admindata, 'status' => 1);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'accountsadd') {
            $accountsret = $accounts->addeditaccounts($data);
            $udata["status"] = $accountsret['status'];
            $udata['msg'] = $accountsret['msg'];
            echo json_encode($udata);
        }
    }

    //收款账号编辑
    public function accountsedit(Request $request)
    {
        $data = $request->param();
        $accounts = new Accounts();
        $lang = $this->lang;
        $admin = session('admin');
        if ($request->isGet()) {
            $info = getinfo($data['id'], 'accounts');
            $country_id = '0';//默认全球
            //不是全球管理员，返回这个职位的国家id
            if ($admin['position_id'] != 1) {
                $admin_arr = explode(",", $info['admin_ids']);
                $country_id = getValue('position', ['id' => $admin['position_id']], 'country_id');
                //不是管理员（所职位的子职位）
                $posid = array_column(getFrames('position', null, $country_id), 'id');
                $posidmap = implode(',', $posid);
                $mappos['position_id'] = array('in', $posidmap);
                //获取的是登录用户的子职位的用户
                $datapos = getables('admin', $mappos, 'id,nickname');
                //剔除没有授权的用户
                foreach ($datapos as $dataposkey => $dataposval) {
                    if (!in_array($dataposval['id'], $admin_arr)) {
                        unset($datapos[$dataposkey]);
                    }
                }
                $info['dname'] = array_values($datapos);
            } else {
                //编辑时，所有的授权员工
                $mapids['id'] = ['in', $info['admin_ids']];
                $info['dname'] = getables('admin', $mapids, 'id,nickname');
            }
            $countrys = getables('country');
            return view('accounts/accountsinfo', [
                'str' => 'accountsedit',
                'info' => $info,
                'lang' => $lang,
                'countrys' => $countrys,
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getbanks'])) {
            echo json_encode(getables('banks', ['country_id' => $data['country_id']]));
        } elseif ($request->isAjax() && $request->isPost() && isset($data['admindata'])) {
            $mapposs['position_id'] = $data['position_id'];//搜索职位id
            $mapposs['nickname'] = ['like', '%' . $data['nickname'] . '%'];
            //获取的是登录用户的子职位的用户
            $admindata = getables('admin', $mapposs, 'id,nickname');
            return array('msg' => $admindata, 'status' => 1);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'accountsedit') {
            $accountsret = $accounts->addeditaccounts($data);
            $udata["status"] = $accountsret['status'];
            $udata['msg'] = $accountsret['msg'];
            echo json_encode($udata);
        }

    }

    //收款账号删除
    public function accountsdel(Request $request)
    {
        $data = $request->param();
        $accounts = new Accounts();
        if ($request->isPost() && $request->isAjax()) {
            $accountsret = $accounts->delaccounts($data);
            $udata["status"] = $accountsret['status'];
            $udata['msg'] = $accountsret['msg'];
            echo json_encode($udata);
        }
    }
}